/* 
 *   Date       Developer   Description
 *   
 *
 *   Classname:  zzz.test.component.bean.Test02
 * 
 *   Date:   2014-3-11
 * 
 *   The source code contained in this listing is proprietary to
 *   HLJ POST YITONG INFO-NET CO.,LTD.
 * 
 *   Unauthorized copying, adaptation, distribution, use, or display
 *   is strictly prohibited.
 *   This software is Copyright 2014 HLJ POST YITONG INFO-NET CO.,LTD.
 */
package com.titan.demo.camel.sample.component.bean;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.JndiRegistry;

import com.titan.demo.camel.utils.CamelUtils;


/**
 *
 *
 * @author Administrator
 *
 */
public class Test02 {

    public static void main(String[] args) {
        CamelContext context = null;
        ProducerTemplate template = null;
        try {
            JndiRegistry jndi = CamelUtils.jndi();
            jndi.bind("one", new MyBean("one"));
            jndi.bind("two", new MyBean("two"));
            jndi.bind("three", new MyBean("three"));
            
            context = new DefaultCamelContext(jndi);
            context.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("direct:start").pipeline("bean:one?method=aaa", "bean:two?method=aaa", "bean:three?method=aaa");

                    from("direct:a").to("bean:one?method=aaa").to("bean:two?method=aaa").to("bean:three?method=aaa").to("mock:a");
                    from("direct:b")
                        .to(CamelUtils.log("a"))
                        .to("bean:one?method=aaa")
                        .to(CamelUtils.log("a"))
                        .to("bean:two?method=aaa")
                        .to(CamelUtils.log("a"))
                        .to("bean:three?method=aaa")
                        .to(CamelUtils.log("a"))
                        .to("mock:b");
                }
            });
            
            
            template = context.createProducerTemplate();
            template.start();
            context.start();
            
            Object response = template.requestBody("direct:start", "Start:");
            System.out.println(response);

            MockEndpoint mockA = context.getEndpoint("mock:a", MockEndpoint.class);
            mockA.expectedMessageCount(1);
            mockA.expectedBodiesReceived("StartA:onetwothree");
            template.sendBody("direct:a", "StartA:");
            mockA.assertIsSatisfied();

            MockEndpoint mockB = context.getEndpoint("mock:b", MockEndpoint.class);
            mockB.expectedMessageCount(1);
            mockB.expectedBodiesReceived("StartB:onetwothree");
            template.sendBody("direct:b", "StartB:");
            mockB.assertIsSatisfied();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CamelUtils.close(template);
            CamelUtils.close(context);
        }
    }

    
    public static class MyBean {
        private String postfix;

        public MyBean(String postfix) {
            this.postfix = postfix;
        }

        public String aaa(String body) {
//            System.out.println(">>>["+body+"]");
            return body + postfix;
        }
    }    
}

